package net.csdn.business.webhook.service.impl;

import com.alibaba.fastjson2.JSON;
import net.csdn.business.common.domain.query.WebhookConfigEventQuery;
import net.csdn.business.common.domain.request.webhook.WebhookNotifyDTO;
import net.csdn.business.common.domain.request.webhook.WebhookRedeliveryDTO;
import net.csdn.business.common.domain.vo.WebhookConfigVO;
import net.csdn.business.common.domain.vo.WebhookLogVO;
import net.csdn.business.common.enums.ResultCodeEnum;
import net.csdn.business.common.enums.RoleEnum;
import net.csdn.business.common.exception.BusinessException;
import net.csdn.business.common.service.SuperServiceImpl;
import net.csdn.business.webhook.entity.WebhookConfigEvent;
import net.csdn.business.webhook.mapper.WebhookConfigEventMapper;
import net.csdn.business.webhook.service.INotifyService;
import net.csdn.business.webhook.service.IWebhookConfigEventService;
import net.csdn.business.webhook.service.IWebhookConfigService;
import net.csdn.business.webhook.service.IWebhookLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.TreeMap;


/**
 * webhook配置事件Service业务层处理
 * 
 * @author zhangyal
 * @date 2023-04-23
 */
@Service
public class WebhookConfigEventServiceImpl extends SuperServiceImpl<WebhookConfigEventMapper, WebhookConfigEvent> implements IWebhookConfigEventService
{

    @Autowired
    private IWebhookConfigService webhookConfigService;

    @Autowired
    private IWebhookLogService webhookLogService;

    @Autowired
    private INotifyService notifyService;

    /**
     * 查询webhook配置事件列表
     * 
     * @param query webhook配置事件
     * @return webhook配置事件
     */
    @Override
    public List<String> selectWebhookConfigEventList(WebhookConfigEventQuery query)
    {
        return baseMapper.selectWebhookConfigEventList(query.getWebHookId());
    }

    @Override
    public Integer insertWebhookConfigEventBatch(List<WebhookConfigEvent> list) {
        return baseMapper.insertBatch(list);
    }


    /**
     * 删除webhook配置事件信息
     * 
     * @param id webhook配置事件主键
     * @return 结果
     */
    @Override
    public Integer deleteWebhookConfigEventById(String id)
    {
        return baseMapper.deleteWebhookEventByWebHookId(id);
    }

    @Override
    public void redelivery(WebhookRedeliveryDTO params) throws Exception {
        String id=params.getId();
        String userId=params.getUserId();
        WebhookLogVO webhookLogVO=webhookLogService.selectWebhookLogById(id);
        if(webhookLogVO==null){
            throw new BusinessException(ResultCodeEnum.PARAMETER_ERROR_CUSTOM,"id");
        }
        String deliveryId=webhookLogVO.getDeliveryId();
        String webhookId=webhookLogVO.getWebHookId();
        WebhookConfigVO webhookConfigVO=webhookConfigService.selectWebhookConfigById(webhookId);
        if(webhookConfigVO==null){
            throw new BusinessException(ResultCodeEnum.WEB_HOOK_REDELIVERY_NO_EXIST_ERROR);
        }
        Integer type=webhookConfigVO.getType();
        String fromId=webhookConfigVO.getFromId();
        switch (type){
            case 1:
                //验证当前用户是否是组织管理员
                String orgRole="";
                if(orgRole==null||!RoleEnum.MAINTAINER.getCode().equals(orgRole)){
                    throw new BusinessException(ResultCodeEnum.UNAUTHORIZED);
                }
                break;
            case 2:
                //验证当前用户是否是项目管理员
                String projectRole=null;
                if(projectRole==null){
                    throw new BusinessException(ResultCodeEnum.UNAUTHORIZED);
                }
                break;
        }
        Integer reqBodyType=webhookConfigVO.getReqBodyType();
        //组装重试请求的参数
        WebhookNotifyDTO notifyParams=new WebhookNotifyDTO();
        notifyParams.setWebhookId(webhookId);
        notifyParams.setEventType(webhookLogVO.getTriggerEvent());
        notifyParams.setReqBodyType(reqBodyType);
        notifyParams.setDeliveryId(deliveryId);
        notifyParams.setSecret(webhookConfigVO.getSecret());
        notifyParams.setPayload(webhookLogVO.getReqData());
        notifyParams.setUrl(webhookConfigVO.getNotifyUrl());
        TreeMap<String,String> headerParams= JSON.parseObject(webhookLogVO.getReqHeaders(), TreeMap.class);
        headerParams.remove("content-type");
        headerParams.remove("Request method");
        headerParams.remove("Request URL");
        headerParams.remove("X-Hub-Signature-256");
        headerParams.remove("X-Hub-Signature");
        switch (reqBodyType){
            case 1:
                headerParams.put("content-type","application/json");
                break;
            case 2:
                headerParams.put("content-type","application/x-www-form-urlencoded");
                break;
        }
        notifyParams.setHeaderParams(headerParams);
        notifyParams.setIsRedelivery(true);
        notifyService.sendRedelivery(notifyParams);
        return;
    }
}
